import { _decorator, Color, Component, EventTouch, ImageAsset, Node, Sprite, SpriteFrame, Texture2D, UITransform, Vec2 } from 'cc';
import { PatternConfig, patternConfigs, sampleColorList } from './Type/Type';
import { Uni8DataMag } from './Uni8DataMag';
const { ccclass, property } = _decorator;

@ccclass('Scene')
export class Scene extends Component {
    private colorTex: Texture2D;


    start() {
       this.initSampleTexture();
       this.initSandTexture()
    }

    initSampleTexture(){
        const image = new ImageAsset(Uni8DataMag.getInstance().assemblySampleTextuteData())
        const tex2d = new Texture2D()
        tex2d.image = image
        tex2d.setFilters(Texture2D.Filter.NEAREST, Texture2D.Filter.NEAREST)
        tex2d.setWrapMode(Texture2D.WrapMode.CLAMP_TO_EDGE, Texture2D.WrapMode.CLAMP_TO_EDGE)
        this.colorTex = tex2d
        const sf = new SpriteFrame()
        sf.texture = tex2d
        sf.packable = false
        const node = new Node()
        node.parent = this.node
        node.addComponent(Sprite).spriteFrame = sf
        node.setPosition(0, 650)
    }
    initSandTexture(){
        const image = new ImageAsset(Uni8DataMag.getInstance().assemblySandTextuteData())
        const tex2d = new Texture2D()
        tex2d.image = image
        tex2d.setFilters(Texture2D.Filter.NEAREST, Texture2D.Filter.NEAREST)
        // tex2d.setWrapMode(Texture2D.WrapMode.CLAMP_TO_EDGE, Texture2D.WrapMode.CLAMP_TO_EDGE)
        const sf = new SpriteFrame()
        // sf.texture = tex2d
        sf.packable = false
        const node = this.node.getChildByName('Sand');
        const sprite = node.getComponent(Sprite)!;
        const mat = sprite.customMaterial || sprite.sharedMaterial;
        mat.setProperty('sandTex', tex2d);
        // console.log(tex2d,'this.colorTex')
        if (this.colorTex) {
            mat.setProperty('colorTex', this.colorTex);
        }
        const size = node.getComponent(UITransform).contentSize;
        mat.setProperty('spriteSize', new Vec2(size.width, size.height));
        // const node = this.
        // node.parent = this.node
        // node.addComponent(Sprite).spriteFrame = sf
        // node.setPosition(0, 650)
    }
    
    // //生成采样数据
    // generationSampleColorBlockData(patternConfig:PatternConfig) {
    //    let {pixelSize,
    //         pattern,
    //         pixelColor,
    //         backgroundColor} = patternConfig;
    //     let sampleColorBlockData:Color[][] = [];
    //     let patternHeight = pattern.length;
    //     let patternWidth = pattern[0].length;
    //     let uni8Width = patternWidth * pixelSize;
    //     let uni8Height = patternHeight * pixelSize;
    //     let uni8Data = new Uint8Array(uni8Width * uni8Height * 4);
    //     //先根据对应配置颜色出数组来
    //     for(let i = 0;i<patternHeight;i++) {
    //         sampleColorBlockData[i] = [];
    //         for(let j = 0;j<patternWidth;j++) {
    //             //==1就填充主颜色，否则就填充背景色
    //             if(pattern[i][j] == 1) {
    //                 sampleColorBlockData[i][j] = new Color(pixelColor[0],pixelColor[1],pixelColor[2],pixelColor[3]);
    //             } else {
    //                 sampleColorBlockData[i][j] = new Color(backgroundColor[0],backgroundColor[1],backgroundColor[2],backgroundColor[3]);
    //             }
    //         }
    //     }
    //     /**
    //      * 上面主颜色确定完就可以开始来填充uni8Data，最后输出uni8Data作为texture的data
    //      */
    //     //先根据uni8Data的格式来填充颜色
    //     for(let h = 0;h<patternHeight;h++) {
    //         for(let w = 0;w<patternWidth;w++) {
    //              //取出颜色
    //             let color = sampleColorBlockData[h][w];
    //             console.log(color,'color')
    //              //算出每个逻辑像素转换到unit8时的大小；
    //             let startX = w * pixelSize;
    //             let startY = h * pixelSize;
    //             let endX = startX + pixelSize;
    //             let endY = startY + pixelSize;
    //             //填充颜色
    //             for(let y = startY;y<endY;y++) {
    //                 for(let x = startX;x<endX;x++) {
    //                     let index = (y * uni8Width + x) * 4;
    //                     uni8Data[index] = color.r;
    //                     uni8Data[index + 1] = color.g;
    //                     uni8Data[index + 2] = color.b;
    //                     uni8Data[index + 3] = color.a;
    //                 }
    //             }

    //         }
    //     }
    //     return uni8Data;
    // }

    update(deltaTime: number) {

    }
}

